#include "complexeditor.h"

#include <QDoubleSpinBox>
#include <QLabel>
#include <QHBoxLayout>
#include <QDebug>

static const int Decimals = 15;
class ComplexEditorPrivate
{
public:
    ComplexEditorPrivate(ComplexEditor* q);

    ComplexEditor* q_ptr;
    QDoubleSpinBox* realSpinBox;
    QDoubleSpinBox* imagSpinBox;
};

ComplexEditorPrivate::ComplexEditorPrivate(ComplexEditor *q) : q_ptr(q)
{
    QHBoxLayout* layout = new QHBoxLayout;
    layout->setSpacing(0);
    layout->setContentsMargins(0, 0, 0, 0);
    q->setContentsMargins(0, 0, 0, 0);
    realSpinBox = new QDoubleSpinBox(q);
    imagSpinBox = new QDoubleSpinBox(q);

    realSpinBox->setDecimals(Decimals);
    imagSpinBox->setDecimals(Decimals);

    realSpinBox->setMinimum(-DBL_MAX);
    realSpinBox->setMaximum(DBL_MAX);
    imagSpinBox->setMinimum(-DBL_MAX);
    imagSpinBox->setMaximum(DBL_MAX);
    imagSpinBox->setSuffix(" i");

    layout->addWidget(realSpinBox);
    layout->addWidget(imagSpinBox);
    q->setLayout(layout);
}

ComplexEditor::ComplexEditor(QWidget* parent) : QWidget(parent)
{
    d_ptr = new ComplexEditorPrivate(this);
}

ComplexEditor::~ComplexEditor()
{
    delete d_ptr;
}

std::complex<double> ComplexEditor::value() const
{
    std::complex<double> v = std::complex<double>(d_ptr->realSpinBox->value(),
                                          d_ptr->imagSpinBox->value());
    return v;
}

void ComplexEditor::setValue(const std::complex<double>& val)
{
    d_ptr->realSpinBox->setValue(val.real());
    d_ptr->imagSpinBox->setValue(val.imag());
}

void ComplexEditor::showEvent(QShowEvent *)
{
    d_ptr->realSpinBox->setFixedHeight(this->height());
    d_ptr->imagSpinBox->setFixedHeight(this->height());
}
